package il.ac.tau.cvws.scrabblesidekick.server.db.template;

import il.ac.tau.cvws.scrabblesidekick.server.db.dao.DictionariesDao;
import il.ac.tau.cvws.scrabblesidekick.server.db.dao.LetterDao;
import il.ac.tau.cvws.scrabblesidekick.server.db.dao.WordsNDao;
import il.ac.tau.cvws.scrabblesidekick.shared.constant.BoardConstants;


public class DBInsertTemplate 
{	
	/*
	 * LOGIC: all the functions insertWord_ insert to their given dictionary the given word. 
	 * insertWord2 inserts all the words with length of two, insert3 inserts all the words with length of three, etc.
	 * INPUT: dictionaryId- an identifier of the dictionary in db, word - an array of integers which represents the numerical indices of the alphbet. 
	 * NOTICE: because the algorithem for finding a word is generic to all the languages all the words are inserted to DB mapped to their numerical index in their alphbet.
	 * RETURN: the function returns the insert string to execute on db.
	 */
	private String insertWord1(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + "," + word[0] +  "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + 1 + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ","  + BoardConstants.EMPTY_TILE + ","+ BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord2(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + "," + word[0] + "," + word[1]  + 
					"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ","  + BoardConstants.EMPTY_TILE + ","+ BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord3(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + "," + word[0] + "," +  word[1] + "," + word[2] + 
				"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ","  + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord4(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + "," +  word[0] + "," +  word[1] + "," + word[2] + "," + word[3] + 
				"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord5(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","  + word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +  "," + word[4] +
				"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord6(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + "," + word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +  "," + word[4] +","+ word[5]+
						"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord7(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + "," + word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +  "," + word[4] +","+ word[5] +"," + word[6] + 
				"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE +")";
	}
	
	private String insertWord8(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
						"," + word[4] +","+ word[5] + "," +  word[6] + "," +  word[7] + 
						"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	} 
	
	private String insertWord9(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] +"," + word[8] + 
					"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord10(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] + "," + word[8] + "," + word[9] +
					"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord11(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] + "," + word[8] + "," 
					+ word[9] + "," +  word[10] +  "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord12(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] + "," + word[8] + "," 
					+ word[9] + "," +  word[10] +  "," +  word[11] +"," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE +")";
	}
	
	private String insertWord13(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] + "," + word[8] + "," 
					+ word[9] + "," +  word[10] +  "," +  word[11] +"," +  word[12] + "," + BoardConstants.EMPTY_TILE + "," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord14(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] + "," + word[8] + "," 
					+ word[9] + "," +  word[10] +  "," +  word[11] +"," +  word[12] + "," +  word[13] +"," + BoardConstants.EMPTY_TILE + ")";
	}
	
	private String insertWord15(int dictionaryId, int[] word)
	{
		return "INSERT INTO "+ WordsNDao.getName() + 
				" VALUES(" + dictionaryId + ","+ word[0] + "," +  word[1] + "," + word[2]+ "," + word[3] +
				"," + word[4] +","+ word[5] + "," +  word[6] +"," +  word[7] + "," + word[8] + "," 
					+ word[9] + "," +  word[10] +  "," +  word[11] +"," +  word[12] + "," +  word[13] + "," +  word[14] +")";
	}
	
	/*
	 * LOGIC: the following function retrieve the insertion string to execute in order to insert the given word. the function calls one of the
	 *  functions above according to the word's length
	 */
	public String insertWord(int dictionaryId, int[] word)
	{
		switch (word.length)
		{
		case 1:  return insertWord1(dictionaryId, word); 
		case 2:  return insertWord2(dictionaryId, word); 
		case 3:  return insertWord3(dictionaryId, word); 
		case 4:  return insertWord4(dictionaryId, word); 
		case 5:  return insertWord5(dictionaryId, word); 
		case 6:  return insertWord6(dictionaryId, word); 
		case 7:  return insertWord7(dictionaryId, word); 
		case 8:  return insertWord8(dictionaryId, word);
		case 9:  return insertWord9(dictionaryId, word);  
		case 10: return insertWord10(dictionaryId, word); 
		case 11: return insertWord11(dictionaryId, word);
		case 12: return insertWord12(dictionaryId, word); 
		case 13: return insertWord13(dictionaryId, word);
		case 14: return insertWord14(dictionaryId, word); 
		case 15: return insertWord15(dictionaryId, word);
		
		default: return null;
		}
	}
	
	/*
	 * LOGIC: the function create a dictionary record in DB. 
	 * INPUT: name- name of language, bLeftRight- 0 means the reading order is not from left to right, otherwise- reading order is from left to right
	 * RETURN: insert string to execute on DB
	 */
	public String insertDictionary(String name, int bLeftRight)
	{
		return "INSERT INTO "+ DictionariesDao.getName() + "(" + DictionariesDao.DICTIONARY_ID + "," + DictionariesDao.NAME + "," + DictionariesDao.IS_LEFT_TO_RIGHT + ")" + 
				" VALUES(" + null + " , '" + name + "' ," +  bLeftRight + ")";
	}
	
	/*
	 * LOGIC: the function inserts a letter from the alphbet of a given dictionary to the letter table in DB.
	 * INPUT: dictionaryId- identifier to dictionary, charcterId- identifier of letter, value- the value of this letter in scrabble game.
	 * RETURN: insert string to execute on DB
	 */
	public String insertLetter(int dictionaryId, int characterId, char character, int value )
	{
		String tempChar = (Character.toString(character));
		return "INSERT INTO "+ LetterDao.getName()  + 
				" VALUES(" + dictionaryId + "," + characterId  + ",'" + tempChar + "'" + "," + value + ")";
	}
	
}
